package com.game.modular.game.battle.process.battleStage.actionPhase.actionPhase2.item;

import com.game.modular.game.battle.info.roundInfo.dto.messageDto.PvpMessageData.SkillReleaseMessage;
import com.game.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.game.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.game.modular.game.playerAssets.domain.PlayerHero;
import com.game.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.game.modular.gameCore.battle.domain.CoreSkillEffect;
import com.game.modular.gameCore.cultivate.domain.CoreSkill;
import com.game.modular.gameCore.cultivate.domain.CoreSkillStar;
import com.game.modular.gameCore.cultivate.service.impl.CoreSkillServiceImpl;
import com.game.modular.gameCore.cultivate.service.impl.CoreSkillStarServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 4
 * 释放技能(选择技能类型--->武将释放技能)
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ReleaseSkillService {
    private final CoreSkillStarServiceImpl coreSkillStarServiceImpl;
    private final CoreSkillServiceImpl coreSkillServiceImpl;
    private final PlayerHeroAttributeServiceImpl attributeService;
    private final ReleaseSkillService1 skillService1;
    private final ReleaseSkillService2 skillService2;
    private final ReleaseSkillService3 skillService3;
    private final ReleaseSkillService4 skillService4;
    private final PlayerHeroAttributeServiceImpl playerHeroAttributeServiceImpl;

    /**
     * 武将释放技能通过 技能类型
     */
    public void heroReleaseSkills(
            @NotNull(message = "战斗id不能为空") Long battleId,
            @NotNull(message = "回合数") Integer roundCount,
            @NotNull(message = "出手数") Integer orderOfAction,
            @NotNull(message = "行动武将不能为空") PlayerHero actWJ,
            @NotNull(message = "武将技能类型 1,主动 2,被动 3,指挥 4,追击") Integer skillType,
            @NotNull(message = "回合内存活武将") List<PlayerHero> actionList
    ) {
        PlayerHeroAttribute attribute = attributeService.get(battleId, actWJ.getId());
        attribute.event.put(skillType, "");
        boolean use = attributeService.statusUseAndRefresh(attribute, 25L);
        if (!use){
            List<PlayerHero> defCamp = actionList.stream()
                    .filter(item -> !item.getRankCamp().equals(actWJ.getRankCamp()))
                    .collect(Collectors.toList());
            List<PlayerHero> actCamp = actionList.stream()
                    .filter(item -> item.getRankCamp().equals(actWJ.getRankCamp()))
                    .collect(Collectors.toList());


            List<SkillReleaseMessage> res = new ArrayList<>();

            Stream<Long> longStream = actWJ.getSkillStarIds().stream().filter(skillStarId -> {
                CoreSkillStar skillStar = coreSkillStarServiceImpl.getCache(skillStarId);
                Long skillId = skillStar.getSkillId();
                return coreSkillServiceImpl.getCache(skillId).getType().equals(skillType);
            });
            longStream.forEach(skillStarId -> {
                SkillReleaseMessage one = heroReleaseSkills(
                        battleId, roundCount, orderOfAction,
                        actWJ, actionList,
                        skillStarId, defCamp, actCamp
                );
                res.add(one);
            });
        }else {
            log.info("---武将{}({})技穷,无法释放{}技能", actWJ.getName(), actWJ.getId(),skillType);
        }
    }




    /**
     * 武将释放技能
     */
    private SkillReleaseMessage heroReleaseSkills(
            Long battleId, Integer roundCount,
            Integer orderOfAction,
            PlayerHero actWJ,
            List<PlayerHero> actionList,
            Long skillStarId,
            List<PlayerHero> defCamp,
            List<PlayerHero> actCamp
    ) {
        CoreSkillStar coreSkillStar = coreSkillStarServiceImpl.getCache(skillStarId);
        CoreSkill coreSkill = coreSkillServiceImpl.getCache(coreSkillStar.getSkillId());
        boolean isRelease = skillService1.determineRelease(battleId, roundCount, orderOfAction, actWJ, coreSkill, coreSkillStar);

        // 基础部分
        Long coreSkillStarId = coreSkillStar.getId();
        Long coreSkillId = coreSkillStar.getSkillId();
        List<PvpSkillEffect> allOneItem = new ArrayList<>();
        SkillReleaseMessage skillReleaseMessage = new SkillReleaseMessage();
        skillReleaseMessage.setSkillId(coreSkillId);
        skillReleaseMessage.setName(coreSkill.getName());
        skillReleaseMessage.setIntro(coreSkill.getIntro());
        skillReleaseMessage.setSkillStarId(coreSkillStarId);
        skillReleaseMessage.setIsRelease(isRelease);

        PlayerHeroAttribute playerHeroAttribute = playerHeroAttributeServiceImpl.get(battleId, actWJ.getId());

        if (isRelease) {
            List<CoreSkillEffect> list = skillService2.list(coreSkillStar);
            for (CoreSkillEffect coreSkillEffect : list) {
                log.info("==========================");
                List<PlayerHero> targetList = skillService2.getTargetHeroes(
                        battleId, coreSkillEffect.getTargetRange(), defCamp, actWJ, actCamp, actionList);

                List<PvpSkillEffect> newSkillEffectList = skillService3.obtainSkillEffects(
                        battleId, coreSkill,coreSkillStar, actWJ, targetList,
                        coreSkillEffect
                );

                List<PvpSkillEffect> oneItem = skillService4.apply(
                        battleId,coreSkill,coreSkillStar, newSkillEffectList
                );

                allOneItem.addAll(oneItem);
                skillReleaseMessage.setNewStatusList(allOneItem);
            }
            // 统计次数
            playerHeroAttribute.logSkillCount(coreSkillStarId);
            playerHeroAttributeServiceImpl.saveCache(playerHeroAttribute);
        }
        return skillReleaseMessage;
    }

}
